/**
  ******************************************************************************
  * @file    sym32l010_hal_def.h
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   Header file of base types and global type definitions.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYM32L010_HAL_DEF_H
#define __SYM32L010_HAL_DEF_H


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "sym32l010.h"

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <assert.h>


/******************************************************************************/
/* Global pre-processor symbols/macros ('#define')                            */
/******************************************************************************/
/** CHIP Info Addr */
#define CHIPINFO_LUID_ADDR                  (0x001007B0UL)
#define CHIPINFO_SUID_ADDR                  (0x001007BAUL)
#define CHIPINFO_PARTNO_ADDR                (0x001007E0UL)
#define CHIPINFO_FLASHSIZE_ADDR             (0x001007D8UL)
#define CHIPINFO_RAMSIZE_ADDR               (0x001007D6UL)
#define CHIPINFO_PINCOUNT_ADDR              (0x001007D5UL)

/** HSI/LSI Trim Code Addr */
#define TRIMCODE_HSI_ADDR                   (0x001007C0UL)
#define TRIMCODE_LSI_ADDR                   (0x001007C2UL)

/** HSI/LSI Trim Code Value */
#define TRIMCODE_HSI_VALUE                  (*(volatile uint16_t *)(TRIMCODE_HSI_ADDR))
#define TRIMCODE_LSI_VALUE                  (*(volatile uint16_t *)(TRIMCODE_LSI_ADDR))

/** SYSCTRL_CR KEY */
#define SYSCTRL_CR_KEY                      (0x53490000UL)

/** FLASH_CR KEY */
#define FLASH_CR_KEY                        (0x53490000UL)


/** Value is true (boolean_t type) */
#ifndef TRUE
  #define TRUE                              ((boolean_t)1UL)
#endif

/** Value is false (boolean_t type) */
#ifndef FALSE
  #define FALSE                             ((boolean_t)0UL)
#endif

/** Shift to the left */
#define bv(n)                               ((uint32_t)0x1UL << (n))

#define bv0                                 (((uint32_t)0x1UL) << 0UL)
#define bv1                                 (((uint32_t)0x1UL) << 1UL)
#define bv2                                 (((uint32_t)0x1UL) << 2UL)
#define bv3                                 (((uint32_t)0x1UL) << 3UL)
#define bv4                                 (((uint32_t)0x1UL) << 4UL)
#define bv5                                 (((uint32_t)0x1UL) << 5UL)
#define bv6                                 (((uint32_t)0x1UL) << 6UL)
#define bv7                                 (((uint32_t)0x1UL) << 7UL)
#define bv8                                 (((uint32_t)0x1UL) << 8UL)
#define bv9                                 (((uint32_t)0x1UL) << 9UL)
#define bv10                                (((uint32_t)0x1UL) << 10U)
#define bv11                                (((uint32_t)0x1UL) << 11UL)
#define bv12                                (((uint32_t)0x1UL) << 12UL)
#define bv13                                (((uint32_t)0x1UL) << 13UL)
#define bv14                                (((uint32_t)0x1UL) << 14UL)
#define bv15                                (((uint32_t)0x1UL) << 15UL)
#define bv16                                (((uint32_t)0x1UL) << 16UL)
#define bv17                                (((uint32_t)0x1UL) << 17UL)
#define bv18                                (((uint32_t)0x1UL) << 18UL)
#define bv19                                (((uint32_t)0x1UL) << 19UL)
#define bv20                                (((uint32_t)0x1UL) << 20UL)
#define bv21                                (((uint32_t)0x1UL) << 21UL)
#define bv22                                (((uint32_t)0x1UL) << 22UL)
#define bv23                                (((uint32_t)0x1UL) << 23UL)
#define bv24                                (((uint32_t)0x1UL) << 24UL)
#define bv25                                (((uint32_t)0x1UL) << 25UL)
#define bv26                                (((uint32_t)0x1UL) << 26UL)
#define bv27                                (((uint32_t)0x1UL) << 27UL)
#define bv28                                (((uint32_t)0x1UL) << 28UL)
#define bv29                                (((uint32_t)0x1UL) << 29UL)
#define bv30                                (((uint32_t)0x1UL) << 30UL)
#define bv31                                (((uint32_t)0x1UL) << 31UL)


/* Exported macro ------------------------------------------------------------*/
//#define USE_FULL_ASSERT
#ifdef  USE_FULL_ASSERT
/**
  * @brief  The assert_param macro is used for function's parameters check.
  * @param  expr If expr is false, it calls assert_failed function
  *         which reports the name of the source file and the source
  *         line number of the call that failed.
  *         If expr is true, it returns no value.
  * @retval None
  */
#define assert_param(expr)                  ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(uint8_t *file, uint32_t line);
#else
#define assert_param(expr)                  ((void)0U)
#endif /* USE_FULL_ASSERT */


/** @brief Reset the Handle's State field.
  * @param __HANDLE__ specifies the Peripheral Handle.
  * @note  This macro can be used for the following purpose:
  *          - When the Handle is declared as local variable; before passing it as parameter
  *            to HAL_PPP_Init() for the first time, it is mandatory to use this macro
  *            to set to 0 the Handle's "State" field.
  *            Otherwise, "State" field may have any random value and the first time the function
  *            HAL_PPP_Init() is called, the low level hardware initialization will be missed
  *            (i.e. HAL_PPP_MspInit() will not be executed).
  *          - When there is a need to reconfigure the low level hardware: instead of calling
  *            HAL_PPP_DeInit() then HAL_PPP_Init(), user can make a call to this macro then HAL_PPP_Init().
  *            In this later function, when the Handle's "State" field is set to 0, it will execute the function
  *            HAL_PPP_MspInit() which will reconfigure the low level hardware.
  * @retval None
  */
#define HAL_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = 0UL)



#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */
#ifndef __weak
#define __weak                              __attribute__((weak))
#endif
#ifndef __packed
#define __packed                            __attribute__((packed))
#endif
#elif defined (__GNUC__) && !defined (__CC_ARM) /* GNU Compiler */
#ifndef __weak
#define __weak                              __attribute__((weak))
#endif /* __weak */
#ifndef __packed
#define __packed                            __attribute__((__packed__))
#endif /* __packed */
#endif /* __GNUC__ */


/* Macro to get variable aligned on 4-bytes, for __ICCARM__ the directive "#pragma data_alignment=4" must be used instead */
#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */
#ifndef __ALIGN_BEGIN
#define __ALIGN_BEGIN
#endif
#ifndef __ALIGN_END
#define __ALIGN_END                         __attribute__ ((aligned (4)))
#pragma clang diagnostic ignored "-Wpadded"
#endif
#elif defined (__GNUC__) && !defined (__CC_ARM) /* GNU Compiler */
#ifndef __ALIGN_END
#define __ALIGN_END                         __attribute__ ((aligned (4)))
#endif /* __ALIGN_END */
#ifndef __ALIGN_BEGIN
#define __ALIGN_BEGIN
#endif /* __ALIGN_BEGIN */
#else
#ifndef __ALIGN_END
#define __ALIGN_END
#endif /* __ALIGN_END */
#ifndef __ALIGN_BEGIN
#if defined   (__CC_ARM)      /* ARM Compiler V5*/
#define __ALIGN_BEGIN                       __align(4)
#elif defined (__ICCARM__)    /* IAR Compiler */
#define __ALIGN_BEGIN
#endif /* __CC_ARM */
#endif /* __ALIGN_BEGIN */
#endif /* __GNUC__ */


/**
  * @brief  __RAM_FUNC definition
  */
#if defined (__CC_ARM) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
/* ARM Compiler V4/V5 and V6
   --------------------------
   RAM functions are defined using the toolchain options.
   Functions that are executed in RAM should reside in a separate source module.
   Using the 'Options for File' dialog you can simply change the 'Code / Const'
   area of a module to a memory space in physical RAM.
   Available memory areas are declared in the 'Target' tab of the 'Options for Target'
   dialog.
*/
#define __RAM_FUNC

#elif defined (__ICCARM__)
/* ICCARM Compiler
   ---------------
   RAM functions are defined using a specific toolchain keyword "__ramfunc".
*/
#define __RAM_FUNC                          __ramfunc

#elif defined   (__GNUC__)
/* GNU Compiler
   ------------
  RAM functions are defined using a specific toolchain attribute
   "__attribute__((section(".RamFunc")))".
*/
#define __RAM_FUNC                          __attribute__((section(".RamFunc")))

#endif

/**
  * @brief  __NOINLINE definition
  */
#if defined (__CC_ARM) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) || defined (__GNUC__)
/* ARM V4/V5 and V6 & GNU Compiler
   -------------------------------
*/
#define __NOINLINE                          __attribute__ ((noinline))

#elif defined (__ICCARM__)
/* ICCARM Compiler
   ---------------
*/
#define __NOINLINE                          _Pragma("optimize = no_inline")

#endif


/******************************************************************************/
/* Global type definitions                                                    */
/******************************************************************************/
/** logical datatype (only values are TRUE and FALSE) */
typedef uint8_t                             boolean_t;

/** single precision floating point number (4 byte) */
typedef float                               float32_t;

/** double precision floating point number (8 byte) */
typedef double                              float64_t;

/** ASCII character for string generation (8 bit) */
typedef char                                char_t;

/** function pointer type to void/void function */
typedef void                                (*func_ptr_t)(void);

/** function pointer type to void/uint8_t function */
typedef void                                (*func_ptr_arg1_t)(uint8_t u8Param);


/** HAL Status structures definition */
typedef enum
{
    HAL_OK       = 0x0UL,
    HAL_ERROR    = 0x1UL,
    HAL_BUSY     = 0x2UL,
    HAL_TIMEOUT  = 0x3UL,
    HAL_OVERFLOW = 0x4UL
} HAL_StatusTypeDef;

/** HAL Lock structures definition */
typedef enum
{
    HAL_UNLOCKED = 0x0UL,
    HAL_LOCKED   = 0x1UL
} HAL_LockTypeDef;

/** HAL Flag Status definition */
typedef enum
{
    RESET        = 0x0UL,
    SET          = 0x1UL
} FlagStatus, ITStatus;

/** HAL Functional State definition */
typedef enum
{
    DISABLE      = 0x0UL,
    ENABLE       = 0x1UL
} FunctionalState;


/******************************************************************************/
/* Global macro function                                                      */
/******************************************************************************/
/** Returns the minimum value out of two values */
#define MINIMUM(X, Y)                       (((X) < (Y)) ? (X) : (Y))

/** Returns the maximum value out of two values */
#define MAXIMUM(X, Y)                       (((X) > (Y)) ? (X) : (Y))

/** Returns the dimension of an array */
#define ARRAY_SZ(X)                         (sizeof(X) / sizeof((X)[0]))

/** Assert for debug */
#ifdef __DEBUG_ASSERT
#define ASSERT(x)                           do { assert((x) > 0UL); } while (0);
#else
#define ASSERT(x)                           {}
#endif


#define HAL_IS_BIT_SET(REG, BIT)            (((REG) & (BIT)) != 0UL)
#define HAL_IS_BIT_CLR(REG, BIT)            (((REG) & (BIT)) == 0UL)
#define HAL_BIT_GET(REG, BITIDX)            (((REG) >> (BITIDX)) & 0x01)

#define UNUSED(X)                           (void)X      /* To avoid gcc/g++ warnings */

#define REGBITS_SET(Reg, Mask)              ((Reg) |= (Mask))
#define REGBITS_CLR(Reg, Mask)              ((Reg) &= (~((uint32_t)(Mask))))
#define REGBITS_GET(Reg, Mask)              ((Reg) & (Mask))
#define REGBITS_MODIFY(Reg, Mask, Value)    ((Reg) = ((Reg) & (~(uint32_t)(Mask))) | ((uint32_t)(Value)))


/******************************************************************************/
/* Exported variables ('extern', definition in C source)                      */
/******************************************************************************/


/******************************************************************************/
/* Global function prototypes ('extern', definition in C source)              */
/******************************************************************************/


#endif /* __SYM32L010_HAL_DEF_H */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
